home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / PCCP024.ARJ / XMODEMS.C < prev    next >
Text File  |  1992-05-17  |  3KB  |  169 lines

  1. /*    Copyright (C) 1992 Peter Edward Cann, all rights reserved.
  2.  *    MicroSoft QuickC: >qcl term.c graphics.lib
  3.  */
  4.  
  5. #include<stdio.h>
  6. #include<bios.h>
  7. #include<dos.h>
  8. #include<fcntl.h>
  9. #include<sys\types.h>
  10. #include<sys\stat.h>
  11. #include<signal.h>
  12. #include"port.h"
  13.  
  14. #define NAK 21
  15. #define ACK 6
  16. #define SOH 1
  17. #define EOT 4
  18. #define CAN 24
  19.  
  20. sendchar(c)
  21.     unsigned char c;
  22.     {
  23.     while(!((inp(basereg+STATREG)&TXMTMASK)&&(inp(basereg+MSTATREG)&CTSMASK)));
  24.     outp(basereg, c);
  25.     }
  26.  
  27. int follow;
  28.  
  29. int rcharto(ticks)
  30.     int ticks;
  31.     {
  32.     long tstamp, tstamp1, dayofticksp;
  33.     int c;
  34.     _bios_timeofday(_TIME_GETCLOCK, &tstamp);
  35.     dayofticksp=0;
  36.     while(1)
  37.         {
  38.         if(_bios_timeofday(_TIME_GETCLOCK, &tstamp1))
  39.             dayofticksp+=20*60*60*24;
  40.         if(tstamp1+dayofticksp-tstamp>ticks)
  41.             return(-1); /* NOTE: This is an INT!!! */
  42.         if(follow!=index)
  43.             {
  44.             c=buf[follow++];
  45.             follow=follow%TBUFSIZ;
  46.             return(c);
  47.             }
  48.         }
  49.     }
  50.  
  51.  
  52. unsigned char block[128];
  53.  
  54. sblock(blockn)
  55.     unsigned char blockn;
  56.     {
  57.     int i;
  58.     unsigned char checksum;
  59.     checksum=0;
  60.     sendchar(SOH);
  61.     sendchar(blockn);
  62.     sendchar((blockn^0xff)&0xff);
  63.     for(i=0;i<128;++i)
  64.         {
  65.         sendchar(block[i]);
  66.         checksum+=block[i];
  67.         }
  68.     sendchar(checksum);
  69.     }
  70.  
  71. quit()
  72.     {
  73.     cleanup();
  74.     exit(99);
  75.     }
  76.  
  77. main(argc, argv)
  78.     int argc;
  79.     char **argv;
  80.     {
  81.     int i, j, infd, ok, c;
  82.     unsigned char blocknum;
  83.     long nbytes;
  84.     index=follow=0;
  85.     printf("Copyright (C) 1992 Peter Edward Cann, all rights reserved.\n");
  86.     printf("xmodem checksum send of %s.\n", argv[4]);
  87.     if(argc!=5)
  88.         {
  89.         printf("USAGE: xmodemr <comnum> <bps> <stopbits> <file pathname>\n");
  90.         exit(1);
  91.         }
  92.     if((infd=open(argv[4], O_RDONLY|O_BINARY))==-1)
  93.         {
  94.         printf("Error opening file %s.\n", argv[4]);
  95.         exit(2);
  96.         }
  97.     comnum=atoi(argv[1])-1;
  98.     speed=atoi(argv[2]);
  99.     databits='8';
  100.     parity='n';
  101.     stopbits=argv[3][0];
  102.     setport();
  103.     signal(SIGINT, quit);
  104.     readset();
  105.     setup();
  106.     nbytes=0;
  107.     if(rcharto(2000)!=NAK)
  108.         {
  109.         printf("Spurrious char or no NAK in 100 seconds.\n");
  110.         cleanup();
  111.         exit(10);
  112.         }
  113.     blocknum=1;
  114.     while(1)
  115.         {
  116.         if((j=read(infd, block, 128))==0)
  117.             {
  118.             printf("End of file.\n");
  119.             sendchar(EOT);
  120.             do
  121.                 c=rcharto(300);
  122.             while((c!=ACK)&&(c!=NAK)&&(c!=-1));
  123.             if(c!=ACK)
  124.                 {
  125.                 printf("\nNo ACK of EOT.\n");
  126.                 cleanup();
  127.                 exit(13);
  128.                 }
  129.             else
  130.                 {
  131.                 printf("\nSuccessful.\n");
  132.                 cleanup();
  133.                 exit(0);    
  134.                 }
  135.             }
  136.         for(c=j;c<128;c++)
  137.             block[c]=26;
  138.         i=0;
  139.         do
  140.             {
  141.             printf("\nSending block %d. ", blocknum);
  142.             sblock(blocknum);
  143.             do
  144.                 c=rcharto(200);
  145.             while((c!=ACK)&&(c!=NAK)&&(c!=CAN)&&(c!=-1));
  146.             }
  147.         while((c==NAK)&&(i++<10));
  148.         if(c!=ACK)
  149.             if(c==NAK)
  150.                 {
  151.                 printf("\nRetry limit exceeded.\n");
  152.                 cleanup();
  153.                 exit(14);
  154.                 }
  155.             else
  156.                 {
  157.                 printf("\nSpurrious character hex %02x; ACK or NAK expected.\n", c);
  158.                 cleanup();
  159.                 exit(11);
  160.                 }
  161.         nbytes+=128;
  162.         printf("Successful. Bytes so far: %ld", nbytes);
  163.         blocknum++;
  164.         }
  165.     printf("Programming error; fell through end; see code.\n");
  166.     cleanup();
  167.     exit(12);
  168.     }
  169.